<?php
/**
 * 启动时间
 * @var mixed
 */
define( 'START_TIME', microtime( TRUE ) );

/**
 * 启动内存
 * @var int 启动所用内存
 */
define( 'START_MEMORY', memory_get_usage() );

/**
 * PHP文件后缀
 * @var string
 */
define( 'EXT', '.php' );

/**
 * 当前时间
 * @var integer
 */
define( 'TIME', time() );

/**
 * 是否WIN系统
 * @var string
 */
define( 'IS_WIN', DIRECTORY_SEPARATOR === '\\' ? true : false );

/**
 * 换行符
 * @var string
 */
define( 'N', "\r\n" );

if ( false ) $dir_system = $dir_project = $dir_wwwroot = $dir_data = $dir_library = $dir_bulider = $dir_shell = $dir_temp = null;
if ( ! isset( $dir_system ) ) $dir_system = dirname( __FILE__ ) . '/../';

/**
 * 系统目录
 * @var string
 */
define( 'DIR_SYSTEM', realpath( $dir_system ) . DIRECTORY_SEPARATOR );

if ( ! isset( $dir_project ) ) $dir_project = DIR_SYSTEM . 'projects/';
if ( ! isset( $dir_wwwroot ) ) $dir_wwwroot = DIR_SYSTEM . 'wwwroot/';
if ( ! isset( $dir_data    ) ) $dir_data    = DIR_SYSTEM . 'data/';
if ( ! isset( $dir_library ) ) $dir_library = DIR_SYSTEM . 'libraries/';
if ( ! isset( $dir_bulider ) ) $dir_bulider = DIR_SYSTEM . 'bulider/';
if ( ! isset( $dir_shell   ) ) $dir_shell   = DIR_SYSTEM . 'shell/';
if ( ! isset( $dir_temp    ) ) $dir_temp    = DIR_SYSTEM . 'temp/';

if ( ! realpath( $dir_project ) )
{
    $dir_project = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . '../';
}
/**
 * 项目目录
 * @var string
 */
define( 'DIR_PROJECT', (realpath( $dir_project ) ? realpath( $dir_project ) : DIR_SYSTEM . 'projects') . DIRECTORY_SEPARATOR );

/**
 * WWWROOR根目录
 * @var string
 */
define( 'DIR_WWWROOT', (realpath( $dir_wwwroot ) ? realpath( $dir_wwwroot ) : DIR_SYSTEM . 'wwwroot') . DIRECTORY_SEPARATOR );

/**
 * 数据目录
 * @var string
 */
define( 'DIR_DATA', (realpath( $dir_data ) ? realpath( $dir_data ) : DIR_SYSTEM . 'data') . DIRECTORY_SEPARATOR );

/**
 * 库文件目录
 * @var string
 */
define( 'DIR_LIBRARY', (realpath( $dir_library ) ? realpath( $dir_library ) : DIR_SYSTEM . 'libraries') . DIRECTORY_SEPARATOR );

/**
 * 系统构建数据目录
 * @var string
 */
define( 'DIR_BULIDER', (realpath( $dir_bulider ) ? realpath( $dir_bulider ) : DIR_SYSTEM . 'bulider') . DIRECTORY_SEPARATOR );

/**
 * 系统构建数据目录
 * @var string
 */
define( 'DIR_SHELL', (realpath( $dir_shell ) ? realpath( $dir_shell ) : DIR_SYSTEM . 'shell') . DIRECTORY_SEPARATOR );

/**
 * 临时文件目录
 * @var string
 */
define( 'DIR_TEMP', (realpath( $dir_temp ) ? realpath( $dir_temp ) : DIR_SYSTEM . 'temp') . DIRECTORY_SEPARATOR );

unset( $dir_system, $dir_project, $dir_wwwroot, $dir_data, $dir_library, $dir_shell, $dir_bulider );

function __load_boot__()
{
    if ( !Bootstrap::$include_path )
    {
        # 当在项目初始化之前发生错误（比如项目不存在），调用系统Core类库
        Bootstrap::$include_path = array(
        	DIR_LIBRARY . 'MyQEE' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR,
    	);
    	# 注册自动加载类
        spl_autoload_register( array( 'Bootstrap', 'auto_load' ) );
    }
}


/**
 * 语言包
 * [strtr](http://php.net/strtr) is used for replacing parameters.
 *
 * __('Welcome back, :user', array(':user' => $username));
 *
 * @uses	I18n::get
 * @param	string  text to translate
 * @param	array   values to replace in the translated text
 * @param	string  target language
 * @return	string
 */
function __( $string, array $values = NULL )
{
    static $have_core = false;
    if ( false===$have_core )
    {
        __load_boot__();
        $have_core = (boolean)class_exists('Core',true);
    }
    if ($have_core)
    {
        $string = Core::i18n()->get( $string );
    }

    return empty( $values ) ? $string : strtr( $string, $values );
}

/**
 * 是否对传入参数转义，建议系统关闭自动转义功能
 * @var boolean
 */
define( 'MAGIC_QUOTES_GPC', get_magic_quotes_gpc() );

if ( MAGIC_QUOTES_GPC )
{

    function _stripcslashes( $string )
    {
        if ( is_array( $string ) )
        {
            foreach ( $string as $key => $val )
            {
                $string[$key] = _stripcslashes( $val );
            }
        }
        else
        {
            $string = stripcslashes( $string );
        }
        return $string;
    }
    $_GET = _stripcslashes( $_GET );
    $_POST = _stripcslashes( $_POST );
    $_COOKIE = _stripcslashes( $_COOKIE );
    $_REQUEST = _stripcslashes( $_REQUEST );
}

/**
 * 系统基础初始化类
 *
 * @author jonwang(jonwang@myqee.com)
 * @category	MyQEE
 * @package		System
 * @subpackage	Core
 * @copyright	Copyright (c) 2008-2011 myqee.com
 * @license		http://www.myqee.com/license.html
 */
abstract class Bootstrap
{

    /**
     * 版本号
     *
     * @var float
     */
    const VERSION = 1.8;

    /**
     * 系统所在的根目录
     *
     * @var string
     */
    public static $base_url = '';

    /**
     * 系统配置
     *
     * @var array
     */
    protected static $config = array();

    /**
     * 所有项目的config配置
     *
     * array('projectName'=>array(...))
     * @var array
     */
    protected static $config_projects = array();

    /**
     * 当前项目
     *
     * @var string
     */
    public static $project;

    /**
     * 当前项目配置
     *
     * @var array
     */
    public static $project_config;

    /**
     * 当前项目目录
     *
     * @var string
     */
    public static $project_dir;

    /**
     * 当前项目的URL
     *
     * @var string
     */
    public static $project_url;

    /**
     * 是否运行于命令行
     *
     * @var string
     */
    public static $is_cli;

    /**
     * 系统文件列表
     *
     * @var array('project_name'=>array(...))
     */
    public static $file_list = array();

    public static $local_debug = false;

    /**
     * 系统请求时的默认pathinfo
     *
     * @var string
     */
    public static $default_pathinfo;

    /**
     * 当前请求是否admin类型请求
     *
     * @var boolean
     */
    public static $is_admin_url = false;

    /**
     * 包含目录
     *
     * array(
     * 	 'test1',
     * 	 'test2',
     * )
     *
     * @var array
     */
    public static $include_path;

    /**
     * 自动加载类
     * @param string $class 类名称
     */
    public static function auto_load( $class )
    {
        if ( class_exists( $class, false ) ) return true;
        static $core_loaded = false;
        if ( ! $core_loaded )
        {
            $core_loaded = class_exists( 'Core', false );
        }
        if ( $core_loaded )
        {
            # 如果Core已加载则采用Core的auto_load方法
            return Core::auto_load( $class );
        }
        $file = 'classes/' . str_replace( '_','/', $class ) . '.class' . EXT;
        if ( isset(self::$file_list[self::$project]) )
        {
            # 读取优化文件列表
            if ( isset( self::$file_list[self::$project][$file] ) )
            {
                require self::$file_list[self::$project][$file];
                return true;
            }
            else
            {
                return false;
            }
        }

        foreach ( self::$include_path as $path )
        {
            $tmpfile = $path . $file;
            if ( is_file( $tmpfile ) )
            {
                require $tmpfile;
                return true;
            }
        }
        return false;
    }

    /**
     * 系统启动函数
     */
    public static function setup( $auto_run = true )
    {
        static $run = null;
        if ( true === $run )
        {
            if ( true === $auto_run )
            {
                Core::setup( true );
            }
            return true;
        }
        $run = true;

        self::$is_cli = (PHP_SAPI === 'cli');

        # 读取系统配置
        if ( !is_file( DIR_SYSTEM . 'config' . EXT ) )
        {
            self::_throw_sys_error_msg( __('Please rename the file config.new:EXT to config:EXT' , array(':EXT'=>EXT)) );
        }
        self::_include_config_file( self::$config['core'] , DIR_SYSTEM.'config'.EXT );

        if ( !self::$is_cli )
        {
            # 输出文件头
            header( 'Content-Type: text/html;charset=' . self::$config['core']['charset'] );
        }

        if ( ! isset( self::$config['core']['projects'] ) || ! self::$config['core']['projects'] )
        {
            self::_throw_sys_error_msg( __('Please create a new project.') );
        }

        $now_project = null;
        if ( self::$is_cli )
        {
            if ( isset($_SERVER['OS']) && $_SERVER['OS']=='Windows_NT' )
            {
                # 切换到UTF-8编码显示状态
                exec('chcp 65001');
            }

            if ( ! isset( $_SERVER["argv"] ) )
            {
                exit( 'Err Argv' );
            }
            $argv = $_SERVER["argv"];
            //$argv[0]为文件名
            if ( isset( $argv[1] ) )
            {
                $project = $argv[1];
                if ( isset( self::$config['core']['projects'][$project] ) )
                {
                    $now_project = $project;
                    $project = self::$config['core']['projects'][$project];
                }
                else
                {
                    $project = false;
                }
            }
            else
            {
                $project = false;
            }

            array_shift( $argv ); //将文件名移除
            array_shift( $argv ); //将项目名移除
            self::$default_pathinfo = trim( implode( '/', $argv ) );
        }
        else
        {
            self::$default_pathinfo = self::_get_pathinfo();
            $project_url = false;
            foreach ( self::$config['core']['projects'] as $k => &$item )
            {
                if ( ! is_array( $item['url'] ) )
                {
                    $item['url'] = array( ( string ) $item['url'] );
                }
                $tmp_pathinfo = self::$default_pathinfo;
                foreach ( $item['url'] as $u )
                {
                    if ( self::_check_is_this_url( $u, self::$default_pathinfo ) )
                    {
                        $project_url = $u;
                        break;
                    }
                }

                if ( false !== $project_url )
                {
                    if ( isset($item['url_admin']) && $item['url_admin'] )
                    {
                        if ( !strpos($item['url_admin'],'://') )
                        {
                            $tmp_pathinfo = self::$default_pathinfo;
                        }
                        if ( self::_check_is_this_url( $item['url_admin'], $tmp_pathinfo ) )
                        {
                            self::$default_pathinfo = $tmp_pathinfo;
                            self::$is_admin_url = true;
                        }
                    }

                    self::$project_url = $project_url;
                    $project_config = $item;
                    $now_project = $k;
                    break;
                }
            }
        }
        if ( ! $now_project )
        {
            if ( self::$is_cli )
            {
                # 命令行下执行
                echo 'use:'.N;
                foreach ( self::$config['core']['projects'] as $k=>$item )
                {
                    if ( isset($item['isuse']) && !$item['isuse'] )continue;
                    echo "    ".$k.N;
                }
                return true;
            }

            if ( isset( self::$config['core']['projects']['default'] ) )
            {
                $now_project = 'default';
            }
            else
            {
                self::_throw_sys_error_msg( __('not found the project: :project',array(':project'=>$now_project)) );
            }
        }

        /**
         * 初始项目名
         * @var string
         */
        define( 'INITIAL_PROJECT_NAME', $now_project );

        self::set_project( $now_project );

        # 注册自动加载类
        spl_autoload_register( array( 'Bootstrap', 'auto_load' ) );

        # 加载系统核心
        Core::setup( $auto_run );
    }

    /**
     * 设置项目
     * 可重新设置新项目已实现程序内项目切换，但需谨慎使用
     * @param string $project
     */
    public static function set_project( $project )
    {
        if ( self::$project == $project )
        {
            return true;
        }
        static $core_config = null;
        if ( null===$core_config )
        {
            # 记录原始Core配置
            $core_config = self::$config['core'];
        }

        if ( ! isset( $core_config['projects'][$project] ) )
        {
            self::_throw_sys_error_msg( __('not found the project: :project.',array(':project'=>$project) ) );
        }
        if ( ! $core_config['projects'][$project]['isuse'] )
        {
            self::_throw_sys_error_msg( __('the project: :project is not open.' , array(':project'=>'$project') ) );
        }

        # 获取core里项目配置
        $project_config = $core_config['projects'][$project];

        # 项目路径
        $project_dir = realpath( DIR_PROJECT . $project_config['dir'] );
        if ( ! $project_dir || ! is_dir( $project_dir ) )
        {
            self::_throw_sys_error_msg( __('the project dir :dir is not exist.' , array(':dir'=>$project_config['dir'])) );
        }
        $project_dir .= DIRECTORY_SEPARATOR;
        self::$project_dir    = $project_dir;

        # 记录所有项目设置，当切换回项目时，使用此设置还原
        static $all_prjects_setting = array();

        # 记录上一个项目设置
        $all_prjects_setting[self::$project] = array(
            'config'         => self::$config,
            'project_config' => self::$project_config,
            'include_path'   => self::$include_path,
            'local_debug'    => self::$local_debug,
            'file_list'      => self::$file_list,
        );

        # 设为当前项目
        self::$project = $project;

        # 记录debug信息
        if ( class_exists( 'Core', false ) )
        {
            Core::debug()->info( '程序已切换到了新项目：' . $project );
        }

        if ( isset($all_prjects_setting[$project]) )
        {
            # 还原配置
            self::$config         = $all_prjects_setting['config'];
            self::$project_config = $all_prjects_setting['project_config'];
            self::$include_path   = $all_prjects_setting['include_path'];
            self::$local_debug    = $all_prjects_setting['local_debug'];
            self::$file_list      = $all_prjects_setting['file_list'];
        }
        else
        {
            # 合并配置
            $config = $core_config['projects'][$project] + self::$config['core'];

            # 读取项目配置
            if ( is_file( $project_dir . 'config' . EXT ) )
            {
                self::_include_config_file( $config, $project_dir . 'config' . EXT );
            }
            # 清理项目配置
            self::$project_config = $config;
            self::$config = array(
            	'core' => & self::$project_config,
            );
            unset($config);


            # DEBUG配置
            if ( self::is_online_debug() )
            {
                self::$local_debug = true;
            }
            elseif ( isset( self::$config['core']['local_debug_cfg'] ) && self::$config['core']['local_debug_cfg'] )
            {
                if ( function_exists( 'get_cfg_var' ) )
                {
                    self::$local_debug = get_cfg_var( self::$config['core']['local_debug_cfg'] ) ? true : false;
                }
                else
                {
                    self::$local_debug = false;
                }
            }
            else
            {
                self::$local_debug = false;
            }


            # Builder构建，处理 self::$file_list
            if ( self::$project_config['use_bulider'] === 'auto' )
            {
                if ( self::$local_debug == true )
                {
                    $usebulider = false;
                }
                else
                {
                    $usebulider = true;
                }
            }
            else
            {
                $usebulider = (boolean)self::$project_config['use_bulider'];
            }
            $project_filelist = DIR_BULIDER . self::$project . DIRECTORY_SEPARATOR . 'project_all_files_list' . EXT;
            if ( true === $usebulider && ! self::$is_cli && is_file( $project_filelist ) )
            {
                # 读取文件列表
                self::_include_config_file( self::$file_list, $project_filelist );
            }


            # 设置包含目录
            self::$include_path = self::get_project_include_path($project);
        }

        if ( isset( self::$project_config['error_reporting'] ) )
        {
            error_reporting( self::$project_config['error_reporting'] );
        }

        # 时区设置
        if ( isset( self::$project_config['timezone'] ) )
        {
            date_default_timezone_set( self::$project_config['timezone'] );
        }

        # 输出调试信息
        if ( self::$local_debug && class_exists( 'Core', false ) )
        {
            Core::debug()->group( '当前加载目录' );
            foreach ( self::$include_path as $value )
            {
                Core::debug()->log( Core::debug_path( $value ) );
            }
            Core::debug()->groupEnd();
        }
    }

    /**
     * 获取指定项目的include_path
     *
     * @param string $project
     * @return array
     */
    protected static function get_project_include_path( $project )
    {
        # 项目目录排第一个
        $library_dir = array( self::$project_dir );

        if ( true === self::$local_debug )
        {
            # 调试类库目录
            $debug_libraries = null;
            if ( isset( self::$project_config['libraries']['debug'] ) )
            {
                $debug_libraries = self::$project_config['libraries']['debug'];
            }
            elseif ( isset( self::$config['core']['libraries']['debug'] ) )
            {
                $debug_libraries = self::$config['core']['libraries']['debug'];
            }
            if ( $debug_libraries )
            {
                if ( ! is_array( $debug_libraries ) )
                {
                    $debug_libraries = array( ( string ) $debug_libraries );
                }
                $debug_path = array();
                foreach ( $debug_libraries as $path )
                {
                    if ( $path[0] == '/' || preg_match( '#^[a-z]:(\\|/).*$#', $path ) )
                    {
                        $path = realpath( $path );
                    }
                    else
                    {
                        $path = realpath( DIR_LIBRARY . $path );
                    }
                    if ( $path )
                    {
                        $debug_path[] = $path . DIRECTORY_SEPARATOR;
                    }
                }
                if ( $debug_path )
                {
                    # 合并
                    $library_dir = array_merge( $library_dir , $debug_path );
                }
            }
        }

        # 自动加载类库
        if ( isset( self::$project_config['libraries']['autoload'] ) )
        {
            $included = (array)self::$project_config['libraries']['autoload'];
        }
        else
        {
        	$included = array();
        }

        if ( self::$is_admin_url )
        {
            # 后台管理加载项
            if ( isset( self::$project_config['libraries']['admin'] ) && is_array( self::$project_config['libraries']['admin'] ) )
            {
                $included = array_merge($included,self::$project_config['libraries']['admin']);
            }
        }

        # 扩展配置
        if ( isset( self::$project_config['excluded'] ) && self::$project_config['excluded'] )
        {
            # 排除的目录
            if ( ! is_array( self::$project_config['excluded'] ) )
            {
                self::$project_config['excluded'] = array( self::$project_config['excluded'] );
            }
            $included = array_diff( $included, self::$project_config['excluded'] );
        }

        foreach ( $included as $path )
        {
            if ( $path[0] == '/' || preg_match( '#^[a-z]:(\\|/).*$#', $path ) )
            {
                $path = realpath( $path );
            }
            else
            {
                $path = realpath( DIR_LIBRARY . $path );
            }
            if ( $path )
            {
                $library_dir[] = $path . DIRECTORY_SEPARATOR;
            }
        }

        # 系统核心库
        $core_dir = array();
        if ( self::$project_config['libraries']['core'] && is_array(self::$project_config['libraries']['core']) )
        {
            foreach ( self::$project_config['libraries']['core'] as $item )
            {
                $core_path = realpath( DIR_LIBRARY . $item );
                if ( $core_path )
                {
                    $core_dir[] = $core_path . DIRECTORY_SEPARATOR;
                }
            }
        }
        if ( ! $core_dir )
        {
            self::_throw_sys_error_msg( __('Core library lost.<br/><br/>Please check the core config.') );
        }
        $library_dir = array_merge($library_dir,$core_dir);

        # 排除重复路径
        $library_dir = array_unique( $library_dir );

        return $library_dir;
    }

    /**
     * 将项目切换回初始项目
     *
     * 当使用Core::set_project()设置切换过项目后，可使用此方法返回初始化时的项目
     */
    public static function reset_project()
    {
        if ( defined( 'INITIAL_PROJECT_NAME' ) && INITIAL_PROJECT_NAME != self::$project )
        {
            self::set_project( INITIAL_PROJECT_NAME );
        }
    }

    /**
     * 返回协议类型
     * 当在命令行里执行，则返回null
     * @return null/http/https
     */
    public static function protocol()
    {
        if ( self::$is_cli )
        {
            return null;
        }
        elseif ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' )
        {
            return 'https';
        }
        else
        {
            return 'http';
        }
    }

    /**
     * 判断是否开启了在线调试
     *
     * @return boolean
     */
    public static function is_online_debug()
    {
        if ( ! isset( $_COOKIE['_debug_open'] ) ) return false;
        if ( ! isset( self::$config['core']['debug_open_password'] ) ) return false;
        if ( ! is_array( self::$config['core']['debug_open_password'] ) ) self::$config['core']['debug_open_password'] = array( ( string ) self::$config['core']['debug_open_password'] );
        foreach ( self::$config['core']['debug_open_password'] as $item )
        {
            if ( $_COOKIE['_debug_open'] == self::get_debug_hash( $item ) )
            {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据密码获取一个hash
     *
     * @param string $password
     * @return string
     */
    public static function get_debug_hash( $password )
    {
        static $config_str = null;
        if ( null === $config_str ) $config_str = var_export( self::$config['core']['debug_open_password'], true );
        return md5( $config_str . '_open$&*@debug' . $password );
    }

    /**
     * 获取指定config文件的数据
     *
     * @param string $file
     * @return array $config
     */
    protected static function _include_config_file( &$config , $file )
    {
        include $file;

        return $config;
    }

    /**
     * 获取path_info
     *
     * @return string
     */
    private static function _get_pathinfo()
    {
        # 处理base_url
        if ( '' == self::$base_url && isset($_SERVER["SCRIPT_NAME"]) && $_SERVER["SCRIPT_NAME"] )
        {
            $base_url_len = strrpos($_SERVER["SCRIPT_NAME"],'/');
            if ( $base_url_len )
            {
                $base_url = substr($_SERVER["SCRIPT_NAME"], 0 , $base_url_len);
                if ( preg_match('#^(.*)/wwwroot$#', $base_url , $m) )
                {
                    # 特殊处理wwwroot目录
                    $base_url = $m[1];
                    $base_url_len = strlen($base_url);
                }
                if ( substr($_SERVER['REQUEST_URI'],0,$base_url_len) == $base_url )
                {
                    self::$base_url = $base_url;
                }
            }
        }

        if ( isset($_SERVER['PATH_INFO']) )
        {
            $pathinfo = $_SERVER["PATH_INFO"];
        }
        else
        {
            if ( isset($_SERVER['REQUEST_URI']) )
            {
                $request_uri = $_SERVER['REQUEST_URI'];
                if ( self::$base_url )
                {
                    $request_uri = substr($request_uri, strlen(self::$base_url));
                }
                // 移除查询参数
                list ( $pathinfo ) = explode( '?', $request_uri, 2 );
            }
            elseif ( isset($_SERVER['PHP_SELF']) )
            {
                $pathinfo = $_SERVER['PHP_SELF'];
            }
            elseif ( isset($_SERVER['REDIRECT_URL']) )
            {
                $pathinfo = $_SERVER['REDIRECT_URL'];
            }
            else
            {
                $pathinfo = false;
            }
        }

        # 过滤pathinfo传入进来的服务器默认页
        if ( false !== $pathinfo && ($indexpagelen = strlen( self::$config['core']['server_index_page'] )) && substr( $pathinfo, - 1 - $indexpagelen ) == '/' . self::$config['core']['server_index_page'] )
        {
            $pathinfo = substr( $pathinfo, 0, - $indexpagelen );
        }
        if ( !isset($_SERVER["PATH_INFO"]) )
        {
            $_SERVER["PATH_INFO"] = $pathinfo;
        }
        $pathinfo = trim( $pathinfo );

        return $pathinfo;
    }

    /**
     * 检查给定的pathinfo是否属于给的的项目内的URL
     *
     * @param string $u 项目的URL路径
     * @param string $pathinfo 给定的Pathinfo
     * @return boolean
     */
    private  static function _check_is_this_url( $u, & $pathinfo )
    {
        if ( $u=='/' )
        {
            return true;
        }
        $u = rtrim( $u, '/' );
        if ( strpos( $u, '://' ) )
        {
            $tmppath = self::protocol() . '://' . $_SERVER["HTTP_HOST"] . '/' . ltrim( $pathinfo, '/' );
        }
        else
        {
            $tmppath = $pathinfo;
        }
        $len = strlen( $u );
        if ( $len > 0 && substr( $tmppath, 0, $len ) == $u )
        {
            $pathinfo = substr( $tmppath, $len );
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 抛出系统启动时错误信息
     * @param string $msg
     */
    private static function _throw_sys_error_msg( $msg )
    {
        __load_boot__();

        # 尝试加载Core类
        if ( class_exists('Core',true) )
        {
            Core::show_500($msg);
        }

        header( 'Content-Type: text/html;charset=utf-8' );

        if ( isset( $_SERVER['SERVER_PROTOCOL'] ) )
        {
            $protocol = $_SERVER['SERVER_PROTOCOL'];
        }
        else
        {
            $protocol = 'HTTP/1.1';
        }

        // HTTP status line
        header( $protocol . ' 500 Internal Server Error' );

        echo $msg;

        exit();
    }
}